package com.qianbei.common.view.foot;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.qianbei.common.base.MyApplication;
import com.qianbei.common.view.foot.HorizontalScrollAdapter.ItemCallback;

import java.util.ArrayList;

public class HorizontalScrollLayout extends LinearLayout implements
		ItemCallback{
	private Context mContext;
	public ViewPager viewPager;
	private int previousViewId, currentViewId;
	public static ArrayList<Integer> iDlist = new ArrayList<Integer>();

	private HorizontalScrollAdapter mAdapter;
	public HorizontalScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
//		previousViewId = R.id.view_icon_home;
//		iDlist.add(R.id.view_icon_home);
//		iDlist.add(R.id.view_icon_services);
//		iDlist.add(R.id.view_icon_traffic);
//		iDlist.add(R.id.view_icon_shopping);
	}

	public void setViewPager(ViewPager viewPager) {
		this.viewPager = viewPager;
	}

	public void setAdapter(HorizontalScrollAdapter adapter) {
		this.mAdapter= adapter;
		mAdapter.callback =this;
		for (int i = 0; i < adapter.getCount(); i++) {
			final View view = adapter.getView(i, null, null);
//			view.setId(iDlist.get(i));

//			view.setOnClickListener(this);
			setOrientation(HORIZONTAL);
			setGravity(Gravity.CENTER_VERTICAL);
			// 布局参数
			int view_width = MyApplication.screenWidth / adapter.getCount();
			int view_height = LayoutParams.MATCH_PARENT;
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					view_width, view_height);
			addView(view, layoutParams);
		}
	}


	private void setCurrentView(int ids) {

		int id = iDlist.indexOf(ids);
		viewPager.setCurrentItem(id, false);
//		(((HomeActivity) mContext).viewPagerSubList.get(id)).visible();
	}

	private void setPreviousView(int ids) {
	
		mAdapter.setImageViewResource(ids);
		int id = iDlist.indexOf(previousViewId);
//		(((HomeActivity) mContext).viewPagerSubList.get(id)).invisible();
	}

	

	@Override
	public void ItemClick(int id) {
		
		currentViewId = id;
		if (previousViewId != currentViewId) {
//			setCurrentView(currentViewId);
//			setPreviousView(previousViewId);
			previousViewId = currentViewId;
		}
		
	}
	
}